package com.chen.utils;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

	/**
	* @author chen
	* @date ${2016-10-30}
	* @version 1.0
	*/

public class SortUtil{  
		
	/*
	 *排序方法 	
	 */
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map sortByComparator(Map unsortMap) {
		 
        List list = new LinkedList(unsortMap.entrySet());
 
        //sort list based on comparator
        Collections.sort(list, new Comparator() 
        {
             public int compare(Object o1, Object o2) 
             {
          return ((Comparable) ((Map.Entry) (o2)).getValue())
          .compareTo(((Map.Entry) (o1)).getValue());
         }
       }
        );
 
        //put sorted list into map again
		Map sortedMap = new LinkedHashMap();
		for (Iterator it = list.iterator(); it.hasNext();) {
		    Map.Entry entry = (Map.Entry)it.next();
		    sortedMap.put(entry.getKey(), entry.getValue());
		}
		return sortedMap;
   
	}
} 
